﻿using B.S.BaseData.Domain;
using MediatR;
using SmartTracking.API.Write.Application.Commands.Purchasing.Deaths;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;

namespace SmartTracking.API.Write.Application.CommandHandlers.Purchasing.Deaths
{
    public class DeleteDeathCommandHandler : IRequestHandler<DeleteDeathCommand, ApiResult<int>>
    {

        private readonly IBaseRepository<Death> death;

        public DeleteDeathCommandHandler(IBaseRepository<Death> death)
        {
            this.death = death;
        }

        public Task<ApiResult<int>> Handle(DeleteDeathCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();

            var ids = death.GetAll().Where(x=>x.DeathId == request.DeathId).FirstOrDefault();

            ids.IsDeleted = true;

            var r = death.UpdateAsync(ids);

            if (r == null)
            {
                res.Message = "删除失败";
                res.Code = ApiEnum.删除失败;
            }
            else
            {
                res.Message = "删除成功";
                res.Code = ApiEnum.删除成功;
            }

            return Task.FromResult(res);

        }
    }
}
